package AAAAA;

public class MyLinkedList implements List{
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    //头插法：

    @Override
    public void addFist(int date) {
        ListNode node =new ListNode(date);
        if(head==null){
            head=node=last;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    }

    //双向链表的尾插法；

    @Override
    public void addLast(int date) {
        ListNode node=new ListNode(date);
        if(head==null){
            head=node=last;
        }else{
            last.next=node;
            node.prev=last;
            last=last.next;
        }
    }

    //计算双向链表的长度；

    @Override
    public int size() {
        ListNode cur=head;
        int count=0;
        while(cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    //寻找指定位置的节点；
    public ListNode findListNode(int index){
        ListNode cur=head;
        while(index!=0){
            index--;
            cur=cur.next;
        }
        return cur;
    }


    @Override
    //向指定位置插入节点；
    public void addIndex(int index, int date) {
        int len=size();
        if(index<0||index>len){
            return;
        }
        if(index==0){
            addFist(date);
            return;
        }
        if(index==len){
            addLast(date);
            return;
        }
        ListNode node=new ListNode(date);
        ListNode cur=findListNode(index);
        node.next=cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;
    }

    //删除双向链表某个值为key的节点；
    @Override
    public void remove(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head!=null){
                        head.next=null;
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur.next!=null){
                        cur.next.prev=cur.prev;
                    }else{
                        last=last.prev;
                    }
                }
                return;
            }
            cur=cur.next;
        }
    }
    //删除所有的值为key的节点；


    @Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head!=null){
                        head.prev=null;
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur.next==null){
                        last=last.prev;
                    }else{
                        cur.next.prev=cur.prev;
                    }
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.prev=null;
            cur.next=null;
            cur=curN;
        }
        head=last=null;
    }

    //打印双向链表；

    @Override
    public void disPlay() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    //判读双向链表中是否包含某个值为key的节点；
    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
}
